<?php require "vuelta.config.php" ?>
<?php require "vuelta_queries.php" ?>
<?php require "print_progress.php" ?>

<!DOCTYPE html>
<html>
<head>
  <title>Vuelta Tourspel - Extra renner opties</title>
  <meta charset="utf-8">
  <script type="text/javascript"></script>
  <script type="text/javascript" src="js/bsn.AutoSuggest_2.1.3_comp.js" charset="utf-8"></script>
  <?php Vuelta::css('css/generic.css');?>
  <?php Site::css('header');?>
  <?php Site::css('login');?>
  <?php Vuelta::css('css/select.css');?>
  <?php Vuelta::css('css/autosuggest.css');?>
</head>

<body>
  <?php Site::load('header'); ?>
  <?php Site::load('login'); ?>
  <div class="content">

    <?php 
      if(!is_logged_in()) {
?>      <p>Je moet ingelogd zijn om deze pagina te bekijken</p> <?php
      }
      else {
        // logged in

        $db = Vuelta::get_database_connection();

        $runners_rev = get_runners_rev($db);
        $chosen_rev = get_chosen_rev($db);

        $climber = get_climber($db);
        $sprinter = get_sprinter($db);
        $top = get_top($db);

        $top_start = 1;
        $top_size = 15;
        $top_end = $top_start + $top_size;
        
        function d_check(&$d) {
          global $runners_rev;
          global $chosen_rev;

          // applies tags to data (empty/no-runner/not-chosen/valid)
          // double entries are a special case

          $d['name'] = strtolower(trim($d['value'])); 

          if($d['name'] == '') {
            $d['tag'] = 'empty';
          }
          elseif(!isset($runners_rev[$d['name']])) {
            $d['tag'] = 'no-runner';
          }
          else {
            //not empty and is a runner
            $d['id'] = $runners_rev[$d['name']];

            if(!isset($chosen_rev[$d['id']])) {
              $d['tag'] = 'not-chosen';
            }
            else {
              $d['tag'] = 'valid';
            }
          }
        }

        $dclimber = Array('value' => isset($climber) ? $climber : "");
        $dsprinter = Array('value' => isset($sprinter) ? $sprinter : "");
        $dtop = Array();
        for($i = $top_start; $i < $top_end; $i++) {
          $dtop[$i] = Array('value' => isset($top[$i]) ? $top[$i] : "");
        }

        if(!empty($_POST)) {
          $dclimber['value'] = pget('climber');
          $dsprinter['value'] = pget('sprinter');
          for($i = $top_start; $i < $top_end; $i++) {
            $dtop[$i]['value'] = pget('top_'.$i);
          }
        }

        d_check($dclimber);
        d_check($dsprinter);
        for($i = $top_start; $i < $top_end; $i++) {
          d_check($dtop[$i]);
        }

        // check for double in top
        $count = Array();
        for($i = $top_start; $i < $top_end; $i++) {
          $cur = &$dtop[$i];
          if(isset($cur['id'])) {
            $id = $cur['id'];

            if(isset($count[$id])) {
              $cur['tag'] = 'double';
              $count[$id]++;
            }
            else {
              $count[$id] = 1;
            }
          }
        }

        if(!empty($_POST)) {
          //update database
          
          unset_climber($db);
          unset_sprinter($db);
          unset_top($db);

          // set all correct runners
          if(isset($dclimber['id'])) {
            set_climber($db, $dclimber['id']);
          }
          if(isset($dsprinter['id'])) {
            set_sprinter($db, $dsprinter['id']);
          }
          for($i = $top_start; $i < $top_end; $i++) {
            if(isset($dtop[$i]['id'])) {
              set_top($db, $dtop[$i]['id'], $i);
            }
          }
        }
        
        print_progress($db, sget('id'));
        $db->close();
 
        function d_tag_to_message($tag) {
          $convert = Array(
            'empty' => 'leeg',
            'no-runner' => 'renner niet gevonden',
            'not-chosen' => 'renner niet in selectie', 
            'valid' => 'goedgekeurd');
          return isset($convert[$tag]) ? $convert[$tag] : $tag;
        }

        $message_col_width = "180px";
?>
    
        <form class="thin-border" action="extra.php" method="POST">
          <small style="display:none;"><input id="hiddenid" disabled="disabled" type="text" /></small>
          <table>
            <caption>beste klimmer</caption>
            <col width="40px" />
            <col/>
            <col width="<?=$message_col_width?>"/>
            <tr>
              <td class="clean"></td>
              <td><input type="text" id="climber" name="climber" value="<?=$dclimber['value']?>" /></td>
              <td align="center" class="<?=$dclimber['tag']?>"><?=d_tag_to_message($dclimber['tag'])?></td>
            </tr>
          </table>


          <table>
            <caption>beste sprinter</caption>
            <col width="40px" />
            <col/>
            <col width="<?=$message_col_width?>"/>
            <tr>
              <td class="clean"></td>
              <td><input type="text" name="sprinter" id="sprinter" value="<?=$dsprinter['value']?>" /></td>
              <td align="center" class="<?=$dsprinter['tag']?>"><?=d_tag_to_message($dsprinter['tag'])?></td>
            </tr>
          </table>

          <table>
            <caption>top 15</caption>
            <col width="40px" />
            <col />
            <col width="<?=$message_col_width?>"/>
            <thead>
            </thead>
            <tfoot>
            </tfoot>
            <tbody>
<?php 
            for($i = $top_start; $i < $top_end; $i++) {
              $cur = &$dtop[$i];
              $value = $cur['value'];
              $tag = $cur['tag'];
              $message = d_tag_to_message($tag);
  ?>          <tr valign="baseline">
                <td><?=$i?></td>
                <td><input type="text" id="top_<?=$i?>" name="top_<?=$i?>" value="<?=$value?>" /></td>
                <td align="center" class="<?=$tag?>"><?=$message?></td>
              </tr>
<?php       }
?>          </tbody>
          </table>
          <p>LET OP! Renners die niet mee doen aan de vuelta (no-runner) kunnen <em>niet</em> worden opgeslagen!</p>
          <p>Indien u van mening bent dat dit niet klopt kunt u contact op nemen met all@mightydev.nl</p>
          <button>Verzend</button>
        </form>
<script type="text/javascript">
  var options = {
    script:"./json.php?",
    varname:"input",
    json:true,
    shownoresults:false,
    maxresults:0,
    callback: function (obj) { document.getElementById('hiddenid').value = obj.id; }
  };
  var as_json_sprinter = new bsn.AutoSuggest('sprinter', options);
  var as_json1_climber = new bsn.AutoSuggest('climber', options);
  <?php for($i = $top_start; $i < $top_end; $i++) { ?>
    var as_json_top_<?=$i?>= new bsn.AutoSuggest('top_<?=$i?>', options);
  <?php } ?>


  </script>
<?php   } 
?></div>
</body>
</html>
